package com.study.compose.beta.activity.coroutines

import android.os.Bundle
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Modifier
import com.module.compose.base.ComposeActivity
import com.module.compose.base.composable.CommonItem
import com.module.compose.base.composable.CommonPage
import com.module.compose.base.utils.ScreenBg
import com.module.compose.base.utils.goTo
import com.module.compose.base.viewModel.PageState
import com.study.compose.beta.R

class CoroutinesActivity : ComposeActivity<CoroutinesViewModel>() {

    //初始化数据
    override fun initData() = viewModel.getBlogList()

    @Composable
    override fun ContentView() {
        /**
         * 把liveData转换为状态，需要依赖 implementation "androidx.compose.runtime:runtime-livedata:$compose_version"
         */
        val blogListData by viewModel.blogListLiveData.observeAsState()
        val pageState by viewModel.pageState.observeAsState(PageState.LOADING)
        /**
         * 使用Compose抽取的通用的页面，还有标题和一个Column布局的内容
         */
        CommonPage(title = "协程", pageState = pageState) {
            /**
             * 使用blogListData创建列表
             */

            LazyColumn(Modifier.fillMaxSize()) {

                items(blogListData!!.size) { index ->
                    CommonItem(
                        content = blogListData!![index].id.toString(),
                        iconId = R.mipmap.icon_android,
                        backgroundColor = ScreenBg,
                    ) {
                        val bundle = Bundle()
                        bundle.putString("id", blogListData!![index].id.toString())
                        goTo(BlogHistoryActivity::class.java, bundle = bundle)
                    }
                }
            }
        }
    }
}
